package com.gwq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwq.dto.ComputedDto;
import com.gwq.dto.ExercisesDto;
import com.gwq.entity.EquationRecord;
import com.gwq.entity.Exercises;
import com.gwq.entity.SysRole;
import com.gwq.entity.SysUser;
import com.gwq.mapper.EquationRecordMapper;
import com.gwq.mapper.ExercisesMapper;
import com.gwq.param.ExercisesPageParam;
import com.gwq.service.*;
import com.gwq.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class ExercisesServicelmpl extends ServiceImpl<ExercisesMapper, Exercises> implements ExercisesService {

    @Autowired
    private ComputerService computerService;
    @Autowired
    private EquationRecordMapper equationRecordMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;


    @Override
    public Exercises addExercises(String strExercises, SysUser sysUser) {
        Exercises exercises = new  Exercises();
        exercises.setExercises(strExercises);
        exercises.setCreatedBy(sysUser.getId());
        exercises.setExTile(sysUser.getUserName()+"——"+DateUtils.getYyyyMMddHHmmss(new Date()));
        exercises.setExercisesStatus(0);
        exercises.setCreatedAt(DateUtils.getYyyyMMddHHmmss(new Date()));
        save(exercises);
        return exercises;
    }

    @Override
    public void updateExercises(String id, String fileName) {
        Exercises exercises = getById(id);
        exercises.setFileName(fileName);
        updateById(exercises);
    }

    @Override
    public Exercises getExercisesById(String id) {
        return getById(id);
    }

    @Override
    public void updateExercises(Exercises exercises) {
        exercises.setCreatedAt(DateUtils.getYyyyMMddHHmmss(new Date()));
        updateById(exercises);
    }

    @Override
    public ExercisesDto getHistoryExercises(String id) {

        Exercises exercises = getById(id);

        ExercisesDto exercisesDto = new ExercisesDto();
        if(exercises!=null){
            QueryWrapper<EquationRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("exercises_id",id);
            queryWrapper.orderByDesc("created_at");
            List<EquationRecord> equationRecords = equationRecordMapper.selectList(queryWrapper);
            if(equationRecords!=null){
                exercisesDto.setId(exercises.getId());
                exercisesDto.setCheckCount(exercises.getCheckCount());
                List<ComputedDto> computedDtos = new ArrayList<>();

                equationRecords.stream().forEach(o->{
                    ComputedDto computedDto = new ComputedDto();
                    computedDto.setSum(o.getSum());
                    computedDto.setRightOrWrong(o.getRightOrWrong());
                    computedDto.setTrueSum(o.getTrueSum());
                    computedDto.setExample(o.getLeftOperand()+o.getOperator()+o.getRightOperand()+"=");
                    computedDto.setId(o.getId());
                    computedDtos.add(computedDto);
                });

                exercisesDto.setComputedDtos(computedDtos);
            }
        }
        return exercisesDto;
    }

    @Override
    public Page<Exercises> listHistoryExercises(ExercisesPageParam exercisesPageParam, Principal principal) {
        SysUser sysUser = sysUserService.getByUserName(principal.getName());
        List<SysRole> sysRoles = sysRoleService.listRolesByUserId(sysUser.getId());
        AtomicReference<Integer> code= new AtomicReference<>(0);
        if(sysRoles!=null){
            sysRoles.stream().forEach(t->{
                if(t.getCode().equalsIgnoreCase("admin")){
                    code.set(1);
                }
            });
        }
        if(code.get()!=1){
            exercisesPageParam.setUserId(sysUser.getId());
        }
        Page<Exercises> page  = new Page<>(exercisesPageParam.getPage(),exercisesPageParam.getSize());
        return (Page<Exercises>) this.baseMapper.listPage(page,exercisesPageParam.getUserName(),exercisesPageParam.getBegTime(),exercisesPageParam.getEndTime(),exercisesPageParam.getUserId());
    }

    @Override
    public Exercises getFileName(String fileName) {
        QueryWrapper<Exercises> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_name",fileName);
        return getOne(queryWrapper);
    }
}
